package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IIndustryAuth;
import com.wbao.dianniu.listener.IIndustryAuthListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

public class IndustryAuthManager implements IIndustryAuth {
	
	private IIndustryAuthListener mListener;
	private Context mContext;

	public IndustryAuthManager(Context context) {
		mContext = context;
	}

	@Override
	public void auth(int id,Integer accountId, String idBeforePic,
			String idAfterPic, String industryPic) {
		// TODO Auto-generated method stub
		StringEntity stringEntity = null;
		try {
			Batch batch = new Batch();
			batch.setToken(GlobalContext.getCacheToken());
			Command command = new Command();
			command.addAction(Action.ACTION_INDUSTRY_AUTH_ADD)
					.addArgs("id", id)
					.addArgs("accountId", accountId)
					.addArgs("idBeforePic", idBeforePic)
					.addArgs("idAfterPic", idAfterPic)
					.addArgs("industryPic", industryPic);
			batch.addCommand(command);
			stringEntity = new StringEntity(batch.toJson(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		HttpUtil http = new HttpUtil();
		http.req(mContext, stringEntity, new MyResponse() {

			@Override
			public void responseSucess(Object obj) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onTwoAuthSuccess();
			}

			@Override
			public void responseFailure(int errorCode, String msg) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onTwoAuthFailure(errorCode, msg);
			}
		});
	}

	@Override
	public boolean addIndustryAuthListener(IIndustryAuthListener listener) {
		mListener = listener;
		return mListener != null;
	}

	@Override
	public boolean removeIndustryAuthListListener(IIndustryAuthListener listener) {
		if (null == listener)
			return false;
		mListener = null;
		return true;
	}

}
